function urlParse() {
  const params = location.search.split('?')[1]
  const obj = {}
  if (params) {
    const paramArr = params.split('&')
    for (let i = 0; i < paramArr.length; i++) {
      const kv = paramArr[i].split('=')
      obj[kv[0]] = kv[1]
    }
  }
  return obj
}
const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
const extendEncryptKey = '' // "1E65E6163E2D38BAD19299679F6387EC";
function encode64(input) {
  input = input + extendEncryptKey
  var output = ''
  var chr1
  var chr2
  var chr3 = ''
  var enc1
  var enc2
  var enc3
  var enc4 = ''
  var i = 0
  do {
    chr1 = input.charCodeAt(i++)
    chr2 = input.charCodeAt(i++)
    chr3 = input.charCodeAt(i++)
    enc1 = chr1 >> 2
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
    enc4 = chr3 & 63
    if (isNaN(chr2)) {
      enc3 = enc4 = 64
    } else if (isNaN(chr3)) {
      enc4 = 64
    }
    output =
      output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4)
    chr1 = chr2 = chr3 = ''
    enc1 = enc2 = enc3 = enc4 = ''
  } while (i < input.length)
  return output
}

// public method for decoding
function decode64(input) {
  var output = ''
  var chr1, chr2, chr3
  var enc1, enc2, enc3, enc4
  var i = 0
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
  while (i < input.length) {
    enc1 = keyStr.indexOf(input.charAt(i++))
    enc2 = keyStr.indexOf(input.charAt(i++))
    enc3 = keyStr.indexOf(input.charAt(i++))
    enc4 = keyStr.indexOf(input.charAt(i++))
    chr1 = (enc1 << 2) | (enc2 >> 4)
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
    chr3 = ((enc3 & 3) << 6) | enc4
    output = output + String.fromCharCode(chr1)
    if (enc3 !== 64) {
      output = output + String.fromCharCode(chr2)
    }
    if (enc4 !== 64) {
      output = output + String.fromCharCode(chr3)
    }
  }
  // output = _utf8_decode(output);
  return output
}

function parseExtext(text) {
  var extObject = null
  if (text && text !== '') {
    text = text.replace(/(=)/g, ':')
    text = text.replace(/(;)/g, '@@')
    var tmp1 = text.split('@@')
    var resJsonArr = []
    for (var i = 0; i < tmp1.length; i++) {
      var item1 = tmp1[i]
      if (item1 !== '') {
        var tmp2 = item1.split(':')
        var field = tmp2[0]
        var fieldvalue = tmp2[1]
        resJsonArr.push('"' + field + '":' + '"' + fieldvalue + '"')
      }
    }
    var json = '{' + resJsonArr.join(',') + '}'
    extObject = JSON.parse(json)
  }
  return extObject
}

/**
 * 日期格式转换
 */
function convertDate(val) {
  var time = ''
  if (val !== '' && val !== undefined) {
    if (val.indexOf('q') !== -1) {
      // 季度转换
      time = DATE2(val, 'XXXX年第X季度')
    } else if (val.indexOf('datap') !== -1) {
      time = DATE3(val, batch_format)
    } else if (val.indexOf('h') !== -1) {
      time = DATE4(val, 'XXXX年X半年')
    } else {
      // 月份转换
      time = DATE(val, 'XXXX年XX月')
    }
  }
  return time
}

/**
 *时间转换
 */
function DATE(date, format) {
  if (date === undefined) date = ''
  var newdate = ''
  if (date.length >= 4) {
    var val1 = date.substring(0, 4)
    newdate = format.replace(/XXXX/, val1)
    if (date.length >= 6) {
      var val2 = date.substring(4, 6)
      if (val2.substring(0, 1) === '0') val2 = val2.substring(1, 2)
      newdate = newdate.replace(/XX/, val2)
      if (date.length === 8) {
        var val3 = date.substring(6, 8)
        if (val3.substring(0, 1) === '0') val3 = val3.substring(1, 2)
        newdate = newdate.replace(/XX/, val3)
      } else {
        // 去掉月份之间的-连接符
        newdate = newdate.replace(/-/, '')
      }
    } else {
      // 如：2017年   把后面的去掉
      newdate = newdate.substring(0, newdate.indexOf('X'))
    }
  }
  if (newdate.indexOf('X') !== -1) {
    newdate = newdate.replace(new RegExp('X', 'gm'), '')
  }
  return newdate
}

/**
 * 时间季度转换
 */
function DATE2(date, format) {
  if (date === undefined) date = ''
  var newdate = ''
  if (date.length >= 4) {
    var val1 = date.substring(0, 4)
    newdate = format.replace(/XXXX/, val1)
    if (date.length >= 6) {
      var val2 = date.substring(5, 6)
      newdate = newdate.replace(/X/, val2)
    }
  }
  if (newdate.indexOf('X') !== -1) {
    newdate = newdate.substring(0, newdate.indexOf('X'))
  }
  return newdate
}
/**
 * 时间批次转换
 */
function DATE3(date, format) {
  if (date === undefined) date = ''
  var newdate = ''
  if (date.length >= 6 && date.indexOf('datap') !== -1) {
    var val1 = date.substring(date.length - 1, date.length)
    newdate = format.replace(/X/, val1)
  }
  return newdate
}

/**
 * 时间年度转换
 */
function DATE4(date, format) {
  if (date === undefined) date = ''
  var newdate = ''
  if (date.length >= 4) {
    var val1 = date.substring(0, 4)
    newdate = format.replace(/XXXX/, val1)
    if (date.length >= 6) {
      var val2 = date.substring(5, 6)
      if (val2 === '1') {
        newdate = newdate.replace(/X/, '上')
      } else {
        newdate = newdate.replace(/X/, '下')
      }
    }
  }
  if (newdate.indexOf('X') !== -1) {
    newdate = newdate.substring(0, newdate.indexOf('X'))
  }
  return newdate
}

/**
 * list转tree
 * */
function listToTree(sNodes, key) {
  var i, l
  var parentKey = 'pid'
  var childKey = 'children'
  if (!key || key === '' || !sNodes) return []
  if (sNodes.length > 0) {
    var r = []
    var tmpMap = []
    for (i = 0, l = sNodes.length; i < l; i++) {
      tmpMap[sNodes[i][key]] = sNodes[i]
    }
    for (i = 0, l = sNodes.length; i < l; i++) {
      if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] !== sNodes[i][parentKey]) {
        if (!tmpMap[sNodes[i][parentKey]][childKey]) {
          tmpMap[sNodes[i][parentKey]][childKey] = []
        }
        tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i])
      } else {
        r.push(sNodes[i])
      }
    }
    return r
  }
}
/**
 * list转tree
 * */
function toTree(data, id, pid) {
  var idkey = id || 'id'
  var pidkey = pid || 'pid'
  data.forEach(function (item) {
    delete item.children
  })
  var map = {}
  data.forEach(function (item) {
    map[item[idkey]] = item
  })
  var val = []
  data.forEach(function (item) {
    var parent = map[item[pidkey]]
    if (parent) {
      ;(parent.children || (parent.children = [])).push(item)
    } else {
      val.push(item)
    }
  })
  return val
}

export { urlParse, encode64, decode64, parseExtext, convertDate, listToTree, toTree }
